package cjj;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import slh.movie.Movie;

@WebServlet("/admin/manageMovies")
public class AdminManageMoviesServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer adminId = (Integer) request.getSession().getAttribute("adminId");
        if (adminId == null) {
            response.sendRedirect("/admin/login");
            return;
        }

        List<Movie> movies = new ArrayList<>();
        try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/webproject_2025", "root", "root");
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM movie")) {

            while (rs.next()) {
                Movie movie = new Movie();
                movie.setMovieId(rs.getInt("MOVIE_ID"));
                movie.setTitle(rs.getString("TITLE"));
                movie.setDirector(rs.getString("DIRECTOR"));
                movie.setReleaseDate(rs.getDate("RELEASE_DATE"));
                movie.setActors(rs.getString("ACTORS"));
                movie.setGenre(rs.getString("GENRE"));
                movie.setCountry(rs.getString("COUNTRY"));
                movie.setLanguage(rs.getString("LANGUAGE"));
                movie.setDuration(rs.getInt("DURATION"));
                movie.setDescription(rs.getString("DESCRIPTION"));
                movie.setCoverImage(rs.getString("COVER_IMAGE"));
                movie.setScore(rs.getDouble("SCORE"));
                movie.setRatingCount(rs.getInt("RATING_COUNT"));
                movies.add(movie);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.setAttribute("movies", movies);
        request.getRequestDispatcher("/cjj/manage_movies.jsp").forward(request, response);
    }
}